﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using BusinessRule.Common;
using System.Configuration;

namespace MesServiceMonitor
{
    public partial class MesServiceMonitor : ServiceBase
    {
        //定时器
        private System.Timers.Timer m_tCamstarAndDataGatWay;//Camstar和DataGatWay服务监控
        //上次定时循环时的xml个数
        private string lastXml;


        public MesServiceMonitor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            LogHelp.WriteLog("服务启动");
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());

            SyncTimerStatr(ref m_tCamstarAndDataGatWay, m_tCamstarAndDataGatWay_Elapsed, intSync * 20 * 60);//10分钟监控一次 *10 * 60
        }


        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync * 1000;
            timer.Enabled = true;
            timer.Start();
        }

        //Camstar和DataGatWay服务监控
        private void m_tCamstarAndDataGatWay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tCamstarAndDataGatWay.Enabled = false;
                CamstarAndDataGatWay cg = new CamstarAndDataGatWay(lastXml);
                cg.RunMain();
                this.lastXml = cg.lastXml;//更新个数
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("Camstar和DataGatWay服务监控", myError);
            }
            finally
            {
                m_tCamstarAndDataGatWay.Enabled = true;
            }
        }

        protected override void OnStop()
        {

        }
    }
}
